import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import './MyButton.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:dio/dio.dart';
class HttpPage extends StatefulWidget {
  HttpPage({Key key}) : super(key: key);

  _HttpPageState createState() => _HttpPageState();
}

class _HttpPageState extends State<HttpPage> {
  List list = [];
  initState(){
    super.initState();
  }
  _getData() async {
    var apiUrl = 'https://xcxt.qzriji.com/server/public/index/index/bookList?uid=6FvyRLTf2I&typeID=4';
    Response result = await Dio().get(apiUrl);
    if(result.statusCode == 200){
      setState(() {
        // print(json.decode(result.data));
        this.list =  result.data['data'];
      }); 
    }else{
      print(result.statusCode);
    }
  }
  _postData() async {
    var apiUrl = 'https://xcxt.qzriji.com/server/public/index/index/bookList';
    var result = await Dio().post(apiUrl,data: {"uid":"6FvyRLTf2I","typeID":"4"});
    if(result.statusCode == 200){ 
      setState(() {
        this.list =  result.data['data'];
      });
    }else{
      print(result.statusCode);
    }
  }
  List<Widget> _getList(){
    var tempList = this.list.map((value){
      return Container(
        child:Column(
          children: <Widget>[
            Image.network(value['bookIcon'],fit: BoxFit.cover,height: 175,width:double.infinity,),
            SizedBox(height: 5),  //设置高度
            Text(
              value['name'],
              textAlign: TextAlign.center,
              overflow:TextOverflow.ellipsis,
              style: TextStyle(
                fontSize: 16
              ),
            )
          ],
        ),
        decoration: BoxDecoration(
          border:Border.all(
            color: Colors.blue
          )
        ),
      ); 
    });
    return tempList.toList();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Http Page"),),
      body: ListView(
          shrinkWrap: true,
          padding: EdgeInsets.all(10),
          children: <Widget>[
                Column(
                  children: <Widget>[
                    MyButton(text: 'Dio().get',pressed: (){
                        _getData(); 
                        Fluttertoast.showToast(
                          msg: this.list[0]['book_name'],
                        );
                    },),
                    MyButton(text: 'Dio().post',pressed: (){
                        _postData();
                        Fluttertoast.showToast(
                          msg: this.list[5]['book_name'],
                        ); 
                    },),
                    
                  ],
                ),
                this.list.length > 0 ? GridView.count(
                    crossAxisSpacing: 10.0,  // 水平之间间距  
                    mainAxisSpacing: 10.0, //   垂直之间间距
                    crossAxisCount: 2, // 一行多少列
                    shrinkWrap: true, 
                    physics: NeverScrollableScrollPhysics(),      // 处理GridView中滑动父级Listview无法滑动
                    children:this._getList()
                  ): Text("")
                ]
            )
        ); 
  }
}

